package work.comeback.omo.util.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static final String path = "/AdminManager";

    public static void addCookie(String name, String value,
                                 int age, HttpServletResponse res) {
        Cookie c = new Cookie(name, value);
        c.setMaxAge(age);
        c.setPath(path);
        res.addCookie(c);
    }

    public static String findCookie(String name,
                                    HttpServletRequest req) {
        String value = null;
        Cookie[] cs = req.getCookies();
        if (cs != null) {
            for (Cookie c : cs) {
                if (c.getName().equals(name)) {
                    value = c.getValue();
                    break;
                }
            }
        }

        return value;
    }

    public static void delCookie(String name,
                                 HttpServletResponse res) {
        Cookie c = new Cookie(name, "");
        c.setPath(path);
        c.setMaxAge(0);
        res.addCookie(c);
    }

    public static String getPath() {
        return path;
    }

    @Override
    public String toString() {
        return "CookieUtil []";
    }
}
